/* Copyright (c) 2010, David Kreuter. See LICENSE for more information */

#include "headers.h"

int read_file(char *filename, uint8 **d, uint32 *len){
	START(read_file);

	if(!filename)goto read_file_error;
	FILE *f=fopen(filename,"rb");
	if(!f)goto read_file_error;
	fseek(f,0,SEEK_END); *len=ftell(f); *d=malloc(*len);
	fseek(f,0,SEEK_SET); fread(*d,*len,1,f);
	fclose(f);

	END(read_file,1);

	ERROR("Couldn't open '%s' for reading\n",filename);
	if(*d)free(*d);*d=0;
	*len=0;
	return 0;
}
int write_file(char *filename, uint8 **d, uint32 *len){
	START(write_file);

	FILE *f=fopen(filename,"wb");
	if(!f)goto write_file_error;
	fwrite(*d,*len,1,f);
	fclose(f);

	END(write_file,1);

	ERROR("Couldn't open '%s' for writing\n",filename);
	return 0;
}
void hexdump(uint8 *buf, uint32 len, char *pre){
	for(uint32 i=0;i<len;i+=8,buf+=8){
		if(len-i==4){
		DEBUG("%s[%4x] %02x %02x %02x %02x\n",
		pre,i,buf[0],buf[1],buf[2],buf[3]);
		}else{
		DEBUG("%s[%4x] %02x %02x %02x %02x  %02x %02x %02x %02x\n",
		pre,i,buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);
		}
	}
}
void nop(char *s, ...){}
uint32 ident=0;
